/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.shop.web.category;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.hk.shop.entity.category.HkShopCategory;
import com.thinkgem.jeesite.modules.hk.shop.service.category.HkShopCategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.shop.entity.category.HkShopCategoryColor;
import com.thinkgem.jeesite.modules.hk.shop.service.category.HkShopCategoryColorService;

import java.util.List;

/**
 * 乐购分类颜色Controller
 * @author 罗天文
 * @version 2018-05-15
 */
@Controller
@RequestMapping(value = "${adminPath}/shop/category/hkShopCategoryColor")
public class HkShopCategoryColorController extends BaseController {
	@Autowired
	private HkShopCategoryService hkShopCategoryService;

	@Autowired
	private HkShopCategoryColorService hkShopCategoryColorService;
	
	@ModelAttribute
	public HkShopCategoryColor get(@RequestParam(required=false) String id) {
		HkShopCategoryColor entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = hkShopCategoryColorService.get(id);
		}
		if (entity == null){
			entity = new HkShopCategoryColor();
		}
		return entity;
	}
	@RequiresPermissions("shop:category:hkShopCategoryColor:view")
	@RequestMapping(value = "index")
	public String index()
	{
		return "hk/shop/category/hkShopCategoryColorIndex";
	}

	@RequiresPermissions("shop:category:hkShopCategoryColor:view")
	@RequestMapping(value = "tree")
	public String tree(Model model)
	{
		List<HkShopCategory> categorys= hkShopCategoryService.findList(new HkShopCategory());
		model.addAttribute("categorys", categorys);
		return "hk/shop/category/hkShopCategoryColorTree";
	}

	@RequiresPermissions("shop:category:hkShopCategoryColor:view")
	@RequestMapping(value = "none")
	public String none()
	{
		return "hk/shop/category/hkShopCategoryColorNone";
	}


	@RequiresPermissions("shop:category:hkShopCategoryColor:view")
	@RequestMapping(value = {"list", ""})
	public String list(HkShopCategoryColor hkShopCategoryColor, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HkShopCategoryColor> page = hkShopCategoryColorService.findPage(new Page<HkShopCategoryColor>(request, response), hkShopCategoryColor);
		model.addAttribute("hkShopCategoryColor", hkShopCategoryColor);
		model.addAttribute("page", page);
		return "hk/shop/category/hkShopCategoryColorList";
	}

	@RequiresPermissions("shop:category:hkShopCategoryColor:view")
	@RequestMapping(value = "form")
	public String form(HkShopCategoryColor hkShopCategoryColor, Model model) {
		model.addAttribute("hkShopCategoryColor", hkShopCategoryColor);
		return "hk/shop/category/hkShopCategoryColorForm";
	}

	@RequiresPermissions("shop:category:hkShopCategoryColor:edit")
	@RequestMapping(value = "save")
	public String save(HkShopCategoryColor hkShopCategoryColor, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, hkShopCategoryColor)){
			return form(hkShopCategoryColor, model);
		}
		hkShopCategoryColorService.save(hkShopCategoryColor);
		addMessage(redirectAttributes, "保存乐购分类颜色成功");
		return "redirect:"+Global.getAdminPath()+"/shop/category/hkShopCategoryColor/?category.id="+hkShopCategoryColor.getCategory().getId();
	}
	
	@RequiresPermissions("shop:category:hkShopCategoryColor:edit")
	@RequestMapping(value = "delete")
	public String delete(HkShopCategoryColor hkShopCategoryColor, RedirectAttributes redirectAttributes) {
		hkShopCategoryColorService.delete(hkShopCategoryColor);
		addMessage(redirectAttributes, "删除乐购分类颜色成功");
		return "redirect:"+Global.getAdminPath()+"/shop/category/hkShopCategoryColor/?repage";
	}

}